<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="问题发现时间" prop="discoveryTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.discoveryTime"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择问题发现时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="来源关联ID" prop="sourceId">-->
<!--        <el-input-->
<!--          v-model="queryParams.sourceId"-->
<!--          placeholder="请输入来源关联ID"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="关联产品ID" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入关联产品ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="关联物料ID" prop="materialId">-->
<!--        <el-input-->
<!--          v-model="queryParams.materialId"-->
<!--          placeholder="请输入关联物料ID"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="不合格品数量" prop="ncrQuantity">-->
<!--        <el-input-->
<!--          v-model="queryParams.ncrQuantity"-->
<!--          placeholder="请输入不合格品数量"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="计量单位" prop="unitOfMeasure">
        <el-input
          v-model="queryParams.unitOfMeasure"
          placeholder="请输入计量单位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主要缺陷类型ID" prop="defectTypeId">
        <el-input
          v-model="queryParams.defectTypeId"
          placeholder="请输入主要缺陷类型ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处置建议" prop="dispositionProposal">
        <el-input
          v-model="queryParams.dispositionProposal"
          placeholder="请输入处置建议"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="处置方案批准人ID" prop="dispositionApprovedBy">-->
<!--        <el-input-->
<!--          v-model="queryParams.dispositionApprovedBy"-->
<!--          placeholder="请输入处置方案批准人ID"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="处置方案批准时间" prop="dispositionApprovalTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.dispositionApprovalTime"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择处置方案批准时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="实际执行处置" prop="actualDisposition">-->
<!--        <el-input-->
<!--          v-model="queryParams.actualDisposition"-->
<!--          placeholder="请输入实际执行处置"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="报告创建人ID" prop="createdBy">
        <el-input
          v-model="queryParams.createdBy"
          placeholder="请输入报告创建人ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报告创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择报告创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最后更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择最后更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['quality-service:reports:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['quality-service:reports:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['quality-service:reports:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['quality-service:reports:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="ncrId" />
      <el-table-column label="关联产品ID" align="center" prop="productId" />
      <el-table-column label="计量单位" align="center" prop="unitOfMeasure" />
      <el-table-column label="主要缺陷类型ID" align="center" prop="defectTypeId" />
      <el-table-column label="问题详细描述" align="center" prop="problemDescription" />
      <el-table-column label="处置建议" align="center" prop="dispositionProposal" />
      <el-table-column label="报告创建人ID" align="center" prop="createdBy" />
      <el-table-column label="报告创建时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后更新时间" align="center" prop="updatedAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="问题发现时间" align="center" prop="discoveryTime" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.discoveryTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="问题来源" align="center" prop="sourceType" />-->
<!--      <el-table-column label="来源关联ID" align="center" prop="sourceId" />-->
<!--      <el-table-column label="关联物料ID" align="center" prop="materialId" />-->
<!--      <el-table-column label="不合格品数量" align="center" prop="ncrQuantity" />-->

<!--      <el-table-column label="初步原因分析" align="center" prop="initialAnalysis" />-->

<!--      <el-table-column label="处置方案细节" align="center" prop="dispositionDetails" />-->
<!--      <el-table-column label="处置方案批准人ID" align="center" prop="dispositionApprovedBy" />-->
<!--      <el-table-column label="处置方案批准时间" align="center" prop="dispositionApprovalTime" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.dispositionApprovalTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="实际执行处置" align="center" prop="actualDisposition" />-->
<!--      <el-table-column label="报告状态" align="center" prop="ncrStatus" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['quality-service:reports:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['quality-service:reports:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改不合格品报告（NCR）管理（记录从发现到关闭的全过程）对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="reportsRef" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="问题发现时间" prop="discoveryTime">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.discoveryTime"-->
<!--            type="date"-->
<!--            value-format="YYYY-MM-DD"-->
<!--            placeholder="请选择问题发现时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="来源关联ID" prop="sourceId">-->
<!--          <el-input v-model="form.sourceId" placeholder="请输入来源关联ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="关联产品ID" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入关联产品ID" />
        </el-form-item>
<!--        <el-form-item label="关联物料ID" prop="materialId">-->
<!--          <el-input v-model="form.materialId" placeholder="请输入关联物料ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="不合格品数量" prop="ncrQuantity">-->
<!--          <el-input v-model="form.ncrQuantity" placeholder="请输入不合格品数量" />-->
<!--        </el-form-item>-->
        <el-form-item label="计量单位" prop="unitOfMeasure">
          <el-input v-model="form.unitOfMeasure" placeholder="请输入计量单位" />
        </el-form-item>
        <el-form-item label="主要缺陷类型ID" prop="defectTypeId">
          <el-input v-model="form.defectTypeId" placeholder="请输入主要缺陷类型ID" />
        </el-form-item>
        <el-form-item label="问题详细描述" prop="problemDescription">
          <el-input v-model="form.problemDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
<!--        <el-form-item label="初步原因分析" prop="initialAnalysis">-->
<!--          <el-input v-model="form.initialAnalysis" type="textarea" placeholder="请输入内容" />-->
<!--        </el-form-item>-->
        <el-form-item label="处置建议" prop="dispositionProposal">
          <el-input v-model="form.dispositionProposal" placeholder="请输入处置建议" />
        </el-form-item>
<!--        <el-form-item label="处置方案细节" prop="dispositionDetails">-->
<!--          <el-input v-model="form.dispositionDetails" type="textarea" placeholder="请输入内容" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="处置方案批准人ID" prop="dispositionApprovedBy">-->
<!--          <el-input v-model="form.dispositionApprovedBy" placeholder="请输入处置方案批准人ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="处置方案批准时间" prop="dispositionApprovalTime">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.dispositionApprovalTime"-->
<!--            type="date"-->
<!--            value-format="YYYY-MM-DD"-->
<!--            placeholder="请选择处置方案批准时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="实际执行处置" prop="actualDisposition">-->
<!--          <el-input v-model="form.actualDisposition" placeholder="请输入实际执行处置" />-->
<!--        </el-form-item>-->
        <el-form-item label="报告创建人ID" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入报告创建人ID" />
        </el-form-item>
        <el-form-item label="报告创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择报告创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择最后更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Reports">
import { listReports, getReports, delReports, addReports, updateReports } from "@/api/quality-service/reports"

const { proxy } = getCurrentInstance()

const reportsList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    discoveryTime: null,
    sourceType: null,
    sourceId: null,
    productId: null,
    materialId: null,
    ncrQuantity: null,
    unitOfMeasure: null,
    defectTypeId: null,
    problemDescription: null,
    initialAnalysis: null,
    dispositionProposal: null,
    dispositionDetails: null,
    dispositionApprovedBy: null,
    dispositionApprovalTime: null,
    actualDisposition: null,
    ncrStatus: null,
    createdBy: null,
    createdAt: null,
    updatedAt: null
  },
  rules: {
    discoveryTime: [
      { required: true, message: "问题发现时间不能为空", trigger: "blur" }
    ],
    sourceType: [
      { required: true, message: "问题来源不能为空", trigger: "change" }
    ],
    ncrQuantity: [
      { required: true, message: "不合格品数量不能为空", trigger: "blur" }
    ],
    unitOfMeasure: [
      { required: true, message: "计量单位不能为空", trigger: "blur" }
    ],
    problemDescription: [
      { required: true, message: "问题详细描述不能为空", trigger: "blur" }
    ],
    dispositionApprovalTime: [
      { required: true, message: "处置方案批准时间不能为空", trigger: "blur" }
    ],
    createdAt: [
      { required: true, message: "报告创建时间不能为空", trigger: "blur" }
    ],
    updatedAt: [
      { required: true, message: "最后更新时间不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询不合格品报告（NCR）管理（记录从发现到关闭的全过程）列表 */
function getList() {
  loading.value = true
  listReports(queryParams.value).then(response => {
    reportsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    ncrId: null,
    discoveryTime: null,
    sourceType: null,
    sourceId: null,
    productId: null,
    materialId: null,
    ncrQuantity: null,
    unitOfMeasure: null,
    defectTypeId: null,
    problemDescription: null,
    initialAnalysis: null,
    dispositionProposal: null,
    dispositionDetails: null,
    dispositionApprovedBy: null,
    dispositionApprovalTime: null,
    actualDisposition: null,
    ncrStatus: null,
    createdBy: null,
    createdAt: null,
    updatedAt: null
  }
  proxy.resetForm("reportsRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.ncrId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加不合格品报告（NCR）管理（记录从发现到关闭的全过程）"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _ncrId = row.ncrId || ids.value
  getReports(_ncrId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改不合格品报告（NCR）管理（记录从发现到关闭的全过程）"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["reportsRef"].validate(valid => {
    if (valid) {
      if (form.value.ncrId != null) {
        updateReports(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addReports(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ncrIds = row.ncrId || ids.value
  proxy.$modal.confirm('是否确认删除不合格品报告（NCR）管理（记录从发现到关闭的全过程）编号为"' + _ncrIds + '"的数据项？').then(function() {
    return delReports(_ncrIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('quality-service/reports/export', {
    ...queryParams.value
  }, `reports_${new Date().getTime()}.xlsx`)
}

getList()
</script>
